<template>
  <div
    class="carousel-item-container"
    ref="container"
    @mousemove="handleMousemove"
    @mouseleave="handleMouseleave"
  >
    <div class="carousel-img" ref="carouselImage" :style="carouselImagePos">
      <ImageLoader
        :src="carousel.bigImg"
        :placeholder="carousel.midImg"
        @load="this.showText"
      ></ImageLoader>
    </div>
    <div class="title" ref="title">{{ carousel.title }}</div>
    <div class="desc" ref="desc">{{ carousel.description }}</div>
  </div>
</template>

<script>
import ImageLoader from "@/components/ImageLoader";


export default {
  name: "CarouselItem",
  data() {
    return {
      titleWidth: 0,
      descWidth: 0,
      containerSize: {
        // 容器的尺寸
        width: 0,
        height: 0,
      },
      carouselImageSize: {
        // 图片的尺寸
        width: 0,
        height: 0,
      },
      mouseX: 0,
      mouseY: 0,
      centerX: 0,
      centerY: 0,
    };
  },
  props: ["carousel"],
  components: {
    ImageLoader,
  },
  computed: {
    // 图片的坐标
    carouselImagePos() {
      // 图片多出来的宽度
      let extraWidth = this.carouselImageSize.width - this.containerSize.width;
      // 图片多出来的高度
      let extraHeight =
        this.carouselImageSize.height - this.containerSize.height;
      /* 
        多出来的宽度 / 容器宽度 = left / 鼠标到容器左边的距离
      */
      let left = -(extraWidth / this.containerSize.width) * this.mouseX;
      let top = -(extraHeight / this.containerSize.height) * this.mouseY;
      return {
        transform: `translate(${left}px, ${top}px)`,
      };
    },
  },
  methods: {
    // * 显示文本
    showText() {
      this.$refs.title.style.opacity = 1;
      this.$refs.title.style.width = 0;
      // 强制让浏览器渲染一次
      this.$refs.title.clientWidth; // reflow
      this.$refs.title.style.transition = "1s";
      this.$refs.title.style.width = this.titleWidth + "px";

      // 描述也是一样
      this.$refs.desc.style.opacity = 1;
      this.$refs.desc.style.width = 0;
      // 强制让浏览器渲染一次
      this.$refs.desc.clientWidth; // reflow
      this.$refs.desc.style.transition = "2s 1s";
      this.$refs.desc.style.width = this.descWidth + "px";
    },
    /**
     * 获取container和图片的尺寸
     */
    setSize() {
      this.containerSize = {
        width: this.$refs.container.clientWidth,
        height: this.$refs.container.clientHeight,
      };
      this.carouselImageSize = {
        width: this.$refs.carouselImage.clientWidth,
        height: this.$refs.carouselImage.clientHeight,
      };
      this.resetMousePos();
    },
    /**
     * * 获取和设置鼠标相对于container的位置
     */
    handleMousemove(event) {
      const rect = this.$refs.container.getBoundingClientRect();
      this.mouseX = event.clientX - rect.left;
      this.mouseY = event.clientY - rect.top;
    },
    handleMouseleave() {
      this.resetMousePos();
    },
    resetMousePos() {
      this.mouseX = this.containerSize.width / 2;
      this.mouseY = this.containerSize.height / 2;
    },
  },
  mounted() {
    this.titleWidth = this.$refs.title.clientWidth;
    this.descWidth = this.$refs.desc.clientWidth;
    this.setSize();
    window.addEventListener("resize", this.setSize);
  },
  destroyed() {
    window.removeEventListener("resize", this.setSize);
  },
};
</script>

<style lang="less" scoped>
.carousel-item-container {
  height: 100%;
  width: 100%;
  position: relative;
  overflow: hidden;
}
.carousel-img {
  position: absolute;
  width: 110%;
  height: 110%;
  z-index: -1;
  transition: 0.2s;
}

.title,
.desc {
  position: absolute;
  left: 60px;
  color: #fff;
  text-shadow: 1px 0 0 rgba(0, 0, 0, 0.5), -1px 0 0 rgba(0, 0, 0, 0.5),
    0 1px 0 rgba(0, 0, 0, 0.5), 0 -1px 0 rgba(0, 0, 0, 0.5);
  letter-spacing: 3px;
  opacity: 0;
  white-space: nowrap;
  overflow: hidden;
}

.title {
  top: calc(50% - 30px);
  font-size: 2rem;
}

.desc {
  top: calc(50% + 20px);
}
</style>